﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace wpf_buildtool
{
    /// <summary>
    /// panel_projfolder.xaml 的交互逻辑
    /// </summary>
    public partial class panel_projfolder : UserControl
    {
        public panel_projfolder()
        {
            InitializeComponent();
        }
        //变化了
        public void OnChange(string path)
        {

            try
            {
                var coll = this.foldertree.Items;

                var dir = new System.IO.DirectoryInfo(path);
                FillCollection(coll, dir);
            }
            catch
            {
                this.foldertree.Items.Clear();
            }
        }
        void FillCollection(ItemCollection coll, System.IO.DirectoryInfo dir)
        {
            foreach (var f in dir.GetFiles())
            {
                TreeViewItem item = new TreeViewItem();
                coll.Add(item);
                item.Header = f.Name;
                item.Tag = f.FullName;
            }
            foreach (var subf in dir.GetDirectories())
            {
                TreeViewItem item = new TreeViewItem();
                coll.Add(item);
                item.Header = "<" + subf.Name + ">";
                FillCollection(item.Items, subf);
            }

        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            Plugin._IDE.GetPipeline("_project").Care(
                (tag, value) =>
                {
                    if (tag == "changeProjPath")
                    {
                        this.OnChange(value);
                    }
                },
                null
            );
        }
        //选中一个项目
        private void Foldertree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            TreeViewItem item = this.foldertree.SelectedItem as TreeViewItem;
            if (item == null)
            {
                Plugin._IDE.SetData_String("__curProj",null);
            }
            else
            {
                var file = item.Tag as string;
                if (String.IsNullOrEmpty(file))
                {

                }
                else
                {
                    Plugin._IDE.OpenFile(file,true);
                }
            }
        }
    }
}
